# -*- encoding:utf-8 -*-
# @Time: 2022/11/22 22:54
# @Author : renyuxuan
# @Email: renyuxuan@maqu.com
# @File: tasks.py
# @Software: PyCharm
import logging
from django.core.mail import send_mail
from celery_tasks.celery_main import app
from renyuxuan_drf27 import settings

logger = logging.getLogger(__name__)

@app.task(name='send_email_task')
def send_email_task(email,username,password,classes_name):
    """
    发送入学通知
    :param email:收件人邮箱
    :param username: 用户名
    :param password: 密码
    :param classes_name: 班级名
    :return:
    """
    try:
        email_param = {
            'subject':'雨轩阁资料库', #邮件标题
            'message':'',#邮件备注信息
            #文本类容
            'html_message':
                f"""
                    <h2>欢迎加入雨轩阁 [{classes_name}] 一起学习成长</h2>
                    <h3>邀请您使用雨轩阁资料库</h3>
                    <p>其中包含：python题库、python笔记资料及购物商城</p>
                    <p>您登录的用户名为<span style='color:green'>{username}</span></p>
                    <p>您的初始密码为<span style='color:green'>{password}</span></p>
                """,
            #发件人
            'from_email':settings.EMAIL_HOST_USER,
            #收件人
            'recipient_list':[email]
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error('雨轩阁身份邮件发送[异常],[email:%s,message:%s]'%(email,e))
    else:
        if res_email:
            logger.info('雨轩阁身份邮件发送[正常],[email:%s,username:%s]'%(email,username))
        else:
            logger.warning('雨轩阁身份邮件发送[失败],[email:%s,username:%s]'%(email,username))

